package com.study.goal.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.utils.StringUtils;
import com.study.goal.domain.WishList;
import com.study.goal.domain.bo.WishListBo;
import com.study.goal.domain.vo.WishListVo;
import com.study.goal.mapper.WishListMapper;
import com.study.goal.service.IWishListService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 愿望清单Service业务层处理
 *
 * @author MT
 * @date 2025-09-03
 */
@RequiredArgsConstructor
@Service
public class WishListServiceImpl implements IWishListService {

    private final WishListMapper baseMapper;

    /**
     * 查询愿望清单
     */
    @Override
    public WishListVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询愿望清单列表
     */
    @Override
    public TableDataInfo<WishListVo> queryPageList(WishListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WishList> lqw = buildQueryWrapper(bo);
        Page<WishListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询愿望清单列表
     */
    @Override
    public List<WishListVo> queryList(WishListBo bo) {
        LambdaQueryWrapper<WishList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WishList> buildQueryWrapper(WishListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WishList> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, WishList::getUserId, bo.getUserId());
        lqw.eq(bo.getGoalId() != null, WishList::getGoalId, bo.getGoalId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), WishList::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), WishList::getDescription, bo.getDescription());
        lqw.eq(bo.getPriority() != null, WishList::getPriority, bo.getPriority());
        lqw.eq(StringUtils.isNotBlank(bo.getImageUrl()), WishList::getImageUrl, bo.getImageUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getUnlockCondition()), WishList::getUnlockCondition, bo.getUnlockCondition());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), WishList::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增愿望清单
     */
    @Override
    public Boolean insertByBo(WishListBo bo) {
        WishList add = BeanUtil.toBean(bo, WishList.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改愿望清单
     */
    @Override
    public Boolean updateByBo(WishListBo bo) {
        WishList update = BeanUtil.toBean(bo, WishList.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WishList entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除愿望清单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
